/* Copyright (c) 2009 SpringShowcase, Inc.
 * All right reserved.
 * http://code.google.com/p/springshowcase/
 * This software is the confidential and proprietary information of BeyondWeb
 * , Inc. You shall not disclose such Confidential Information and
 * shall use it only in accordance with the terms of the license agreement
 * you entered into with BeyondWeb.
 *
 * Revision History
 * Author              Date             Description
 * ------------------   --------------    ------------------
 *  taeki.kim           2010. 4. 6.        
 */
package org.springshowcase.core.mvc;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.MessageSource;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springshowcase.core.commons.utils.ControllerUtils;

public class BindingResultSupport {
	// ---------------------------------------------------------------------
	// Class Variable Declaration
	// ---------------------------------------------------------------------
	private static final Logger logger = LoggerFactory
			.getLogger(BindingResultSupport.class);

	// ---------------------------------------------------------------------
	// Constructor Method Declaration
	// ---------------------------------------------------------------------
	// ---------------------------------------------------------------------
	// DI Method (setter,getter) Declaration
	// ---------------------------------------------------------------------
	// ---------------------------------------------------------------------
	// this Method (public/private) Declaration
	// ---------------------------------------------------------------------
	/**
	 * Gets the bind error messages.
	 * 
	 * @param bindingResult the binding result
	 * @param messageSource the message source
	 * @return the bind error messages
	 * @throws Exception the exception
	 */
	public static List<BindingErrorMessageBean> getBindErrorMessages(
			BindingResult bindingResult, MessageSource messageSource)
			throws Exception {
		List<BindingErrorMessageBean> messageList = new ArrayList<BindingErrorMessageBean>();

		if (bindingResult == null || !bindingResult.hasErrors()) {
			return messageList;
		}

		List<FieldError> fieldErrorList = bindingResult.getFieldErrors();

		for (FieldError error : fieldErrorList) {
			BindingErrorMessageBean bemb = new BindingErrorMessageBean();
			String message = error.getDefaultMessage();
			String[] messageCodes = error.getCodes();
			if (messageCodes[0] != null) {
				message = messageSource.getMessage(messageCodes[1], null, error
						.getDefaultMessage(), ControllerUtils
						.getCurrentLocale());
			}
			bemb.setHtmlTagId(error.getField());
			bemb.setMessage(message);
			messageList.add(bemb);
		}
		return messageList;
	}
	// ---------------------------------------------------------------------
	// interface implement Method (public/private) Declaration
	// ---------------------------------------------------------------------
	// ---------------------------------------------------------------------
	// abstract implement Method (public/private) Declaration
	// ---------------------------------------------------------------------
	// ---------------------------------------------------------------------
	// abstract Method (public/private) Declaration
	// ---------------------------------------------------------------------
}
